package com.simple.cloud.mapper;

import com.mybatisflex.core.BaseMapper;
import com.simple.cloud.domain.entity.SysRole;
import com.simple.cloud.domain.entity.SysUser;
import com.simple.cloud.domain.resp.SysUserResp;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Set;


@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {
    @Delete("<script>DELETE FROM sys_user_role  " +
            "<where>" +
            "<foreach collection='ids' item='id' open='user_id in (' separator=',' close=')'>#{id}</foreach>" +
            "</where>" +
            "</script>")
    void deleteUserRoleRelationByUserIds(@Param("ids") List<Long> ids);

    @Insert("<script>" +
            "INSERT INTO sys_user_role (user_id,role_id) VALUES " +
            "<foreach collection='users' item='user' separator=','>(#{user.id},#{user.role.id})</foreach>" +
            "</script>")
    void saveUserRoleRelations(@Param("users") List<SysUser> users);


    @Select("<script>" +
            "select id,username,password,real_name,telephone,email,last_login_time,status,deleted,remark," +
            "creator,created_time,editor,edited_time from sys_user " +
            "where username = #{username}" +
            "</script>")
    @Results(id = "userRoleMenuResultMap", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "id", property = "role", javaType = SysRole.class,
                    one = @One(select = "com.qcsec.mapper.SysRoleMapper.selectRoleByUserId", fetchType = FetchType.EAGER)),
            @Result(column = "id", property = "menus", javaType = Set.class,
                    many = @Many(select = "com.qcsec.mapper.SysMenuMapper.selectMenusByUserId", fetchType = FetchType.EAGER))
    })
    SysUserResp selectUserByUsername(@Param("username") String username);

    @Select("select id,username,real_name,telephone,email,last_login_time,status,deleted,remark," +
            "creator,created_time,editor,edited_time " +
            "from sys_user where id =#{id}")
    @ResultMap("userRoleMenuResultMap")
    SysUserResp selectUserByUserId(@Param("id") Long id);


    // =====================================自定义分页 start ================================================================
//  使用方法 ：sysUserMapper.xmlPaginate("selectUserPage",page, wrapper) ；
//  第一个参数是自定义sql方法名，第二个参数是分页对象，第三个参数是查询条件
//  selectUserPage是数据查询语句，
//  selectUserPage_COUNT 是count查询函数，xxxx_COUNT:是固定的。
    @Select("<script>" +
            "select id,username,real_name,telephone,email,last_login_time,status," +
            "deleted,remark,creator,created_time,editor,edited_time from sys_user " +
            "${qwSql} limit ${pageOffset}, ${pageSize} " +
            "</script>")
    @ResultMap("userRoleMenuResultMap")
    List<SysUserResp> selectUserPage();

    @Select("<script>" +
            "select count(*) from sys_user ${qwSql}" +
            "</script>")
    long selectUserPage_COUNT();
// =====================================自定义分页 end ================================================================
}
